{include file="public/_meta" /}
<body>
<style>
    .required_b{
        color: red;
    }
</style>
<div class="wrapper">

    {include file="public/_header" /}

    <!-- Sidebar -->
    {include file="public/_menu" /}
    <!-- End Sidebar -->

    <div class="main-panel">

        <div class="content">
            <div class="page-inner">

                <!-- 面包屑 -->
                {include file="public/_page_header" /}

                <div class="row">
                    <div class="col-md-12">
                        <div class="card">
                            <div class="card-header">
                                <div class="d-flex align-items-center">
                                    <h4 class="card-title">列表</h4>

                                    <button class="btn btn-primary btn-round ml-auto" data-toggle="modal" data-target="#addRowModal" onClick="add_item(0)">
                                        <i class="fa fa-plus"></i>
                                        添加
                                    </button>

                                </div>

                                <form class="row">

                                    <div class="col-md-6 col-lg-2">
                                        <div class="form-group">
                                            <input type="text" class="form-control" name="acc_name" value="{$acc_name}" placeholder="账户名称">
                                        </div>
                                    </div>

                                    <div class="col-md-6 col-lg-2">
                                        <div class="form-group">
                                            <input type="text" class="form-control" name="adv_id" value="{$adv_id}" placeholder="账户ID">
                                        </div>
                                    </div>

                                    <div class="col-md-6 col-lg-2">
                                        <div class="form-group">
                                            <input type="text" class="form-control" name="cityname" value="{$cityname}" placeholder="请输入城市">
                                        </div>
                                    </div>

                                    <div class="col-md-6 col-lg-2">
                                        <div class="form-group">

                                            <select class="form-control input-full" name="pro_id" id="pro_id_s">
                                                <option value="">全部产品</option>
                                                {volist name="products" id="vo"}
                                                <option value="{$key}">{$vo}</option>
                                                {/volist}
                                            </select>
                                        </div>
                                    </div>

                                    <div class="col-md-6 col-lg-2">
                                        <div class="form-group">
                                            <select class="form-control" name="status" id="status_s">
                                                <option value="" >全部状态</option>
                                                <option value="1" >启用</option>
                                                <option value="0" >禁用</option>
                                            </select>
                                        </div>
                                    </div>



                                    <div class="col-md-6 col-lg-2">
                                        <div class="form-group">
                                            <button type="submit" class="btn btn-primary">查询</button>
                                            <a href="{:url('clueCost/index')}" class="btn btn-info" style="color: white;position: absolute;left: 110px;">重置</a>
                                        </div>
                                    </div>

                                </form>

                            </div>


                            <div class="card-body">
                                <!-- Modal -->
                                <div class="modal fade" id="addRowModal" tabindex="-1" role="dialog" aria-hidden="true">
                                    <div class="modal-dialog" role="document">
                                        <div class="modal-content">
                                            <div class="modal-header no-bd">
                                                <h5 class="modal-title">
                                                    <span class="fw-mediumbold">添加</span>

                                                </h5>
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                    <span aria-hidden="true">&times;</span>
                                                </button>
                                            </div>
                                            <div class="modal-body">
                                                <form id="form-modal">
                                                    <div class="row">


                                                        <div class="form-group form-inline col-md-12">
                                                            <label for="account_id" class="col-md-3 col-form-label"><font style="vertical-align: inherit;">账户</font></label>
                                                            <div class="col-md-8 p-0">
                                                                <select class="form-control input-full" name="account_id" id="account_id">
                                                                    <option value="0">--请选择账户--</option>
                                                                    {volist name="account" id="vo"}
                                                                    <option value="{$vo.id}">【{$vo.acc_name}】【{$vo.acc_desc}】【{$vo.adv_id}】</option>
                                                                    {/volist}
                                                                </select>

                                                            </div>
                                                        </div>

                                                        <div class="form-group form-inline col-md-12">
                                                            <label for="pro_id" class="col-md-3 col-form-label"><font style="vertical-align: inherit;">投放产品</font></label>
                                                            <div class="col-md-8 p-0">
                                                                <select class="form-control input-full" name="pro_id" id="pro_id">
                                                                    <option value="0">--选择投放产品-- </option>
                                                                    {volist name="products" id="vo"}
                                                                    <option value="{$key}">{$vo}</option>
                                                                    {/volist}
                                                                </select>
                                                            </div>
                                                        </div>


                                                        <div class="form-group form-inline col-md-12">
                                                            <label for="province" class="col-md-3 col-form-label"><font style="vertical-align: inherit;">省份/直辖市</font></label>
                                                            <div class="col-md-8 p-0">
                                                                <select class="form-control input-full" name="province" id="province" onchange="loadRegion('province',2,'city')">
                                                                    <option value="0">省份/直辖市</option>
                                                                    {volist name="province" id="vo"}
                                                                    <option value="{$vo.id}">{$vo.name}</option>
                                                                    {/volist}
                                                                </select>
                                                            </div>
                                                        </div>

                                                        <div class="form-group form-inline col-md-12">
                                                            <label for="city" class="col-md-3 col-form-label"><font style="vertical-align: inherit;">市/县</font></label>
                                                            <div class="col-md-8 p-0">
                                                                <select class="form-control input-full" name="city" id="city" onchange="loadRegion('city',3,'town')">
                                                                    <option value="0">市/县</option>
                                                                </select>
                                                            </div>
                                                        </div>

                                                        <div class="form-group form-inline col-md-12">
                                                            <label for="up_limit" class="col-md-3 col-form-label"><font style="vertical-align: inherit;">成本金额</font></label>
                                                            <div class="col-md-8 p-0">
                                                                <input type="text" class="form-control input-full" id="money" name="money" placeholder="成本金额">
                                                            </div>
                                                        </div>




                                                        <div class="form-check form-inline col-md-12">
                                                            <div class="col-md-8 p-0">
                                                                <div class="form-check">
                                                                    <label class="col-md-3 col-form-label"><font style="vertical-align: inherit;">状态</font></label>
                                                                    <label class="form-radio-label">
                                                                        <input class="form-radio-input" type="radio" name="status" value="1" checked="">
                                                                        <span class="form-radio-sign">启用</span>
                                                                    </label>
                                                                    <label class="form-radio-label ml-3">
                                                                        <input class="form-radio-input" type="radio" name="status" value="0">
                                                                        <span class="form-radio-sign">禁用</span>
                                                                    </label>
                                                                </div>
                                                            </div>
                                                        </div>


                                                    </div>
                                                    <div class="modal-footer no-bd">
                                                        <input type="hidden" id="id" name="id" value="">
                                                        <button type="submit" id="addRowButton" class="btn btn-primary">保存</button>
                                                        <button type="button" class="btn btn-danger" data-dismiss="modal">关闭</button>
                                                    </div>
                                                </form>
                                            </div>

                                        </div>
                                    </div>
                                </div>


                                <div class="table-responsive">
                                    <table id="add-row" class="display table table-striped table-hover" >
                                        <thead>
                                        <tr>
                                            <th>ID</th>
                                            <th>账户</th>
                                            <th>产品</th>
                                            <th>城市</th>
                                            <th>成本</th>
                                            <th>状态</th>
                                            <th>时间</th>
                                            <th style="width: 10%">操作</th>
                                        </tr>
                                        </thead>

                                        <tbody>

                                        {volist name="infos" id="vo"}
                                        <tr>
                                            <td>{$vo.id}</td>
                                            <td>
                                                账户ID：【{$vo.adv_id}】<br>
                                                名称：{$vo.acc_name}<br>
                                                简称：{$vo.acc_desc}
                                            </td>
                                            <td>{$vo.pro_name}</td>
                                            <td>{$vo.city}</td>
                                            <td>{$vo.money}</td>
                                            <td>{$vo.status|getStatus}</td>
                                            <td>创建时间：{$vo.create_time}<br>
                                                修改时间：{$vo.update_time}
                                            </td>


                                            <td>
                                                <div class="form-button-action">


                                                    {if condition="$vo.status eq 1"}

                                                    <button type="button" data-toggle="tooltip" title="" onClick="admin_stop(this,'{$vo.id}')" class="btn btn-link btn-warning" data-original-title="停用">
                                                        <i class="fa fa-toggle-on"></i>停用
                                                    </button>


                                                    {else/}

                                                    <button type="button" data-toggle="tooltip" title="" onClick="admin_start(this,'{$vo.id}')" class="btn btn-link btn-success" data-original-title="启用">
                                                        <i class="fa fa-toggle-off"></i>启用
                                                    </button>
                                                    {/if}

                                                    <button type="button" title="编辑" class="btn btn-link btn-primary btn-lg" data-toggle="modal" data-target="#addRowModal" onClick="edit_item('{$vo.id}')">
                                                        <i class="fa fa-edit"></i>编辑
                                                    </button>

                                                    <button type="button" data-toggle="tooltip" onClick="admin_del(this,'{$vo.id}')" title="" class="btn btn-link btn-danger" data-original-title="删除">
                                                        <i class="fa fa-times"></i>删除
                                                    </button>

                                                </div>



                                            </td>

                                        </tr>
                                        {/volist}

                                        </tbody>
                                    </table>

                                    <!-- 分页 (调取通用方法)-->
                                    <div class="layui-box layui-laypage layui-laypage-molv">{:pagination($infos)}</div>

                                </div>
                            </div>
                        </div>
                    </div>
                </div>


            </div>
        </div>

        {include file="public/_footer" /}


    </div>



</div>

{include file="public/_js" /}

</body>

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript">
    var SCOPE = {
        'status_url':"{:url('ClueCost/status')}",
        'delete_url':"{:url('ClueCost/delete')}",
        'item_url':"{:url('ClueCost/edit')}",
        // 'edit_daytime':"{:url('merchant/edit_daytime')}",
        'item_mapping_url':"{:url('merchant/edit_mapping')}",
        'add_url':"{:url('ClueCost/save')}",
        'add_timing_url':"{:url('merchant/save_timing')}",
        'add_timing_url2':"{:url('merchant/save_timing2')}",
        'add_timeday':"{:url('merchant/save_timeday')}",
        'add_timeday2':"{:url('merchant/save_timeday2')}",
        'add_mapping_url':"{:url('merchant/save_mapping')}",
        'clue_url':"{:url('merchant/clue_url')}",
        'send_phone':"{:url('merchant/sendSmsPhone')}",
        'city_url':"{:url('clue/getRegion')}",
    };

    $('#pro_id_s').val('{$pro_id}')
    $('#status_s').val('{$status}')

    function loadRegion(sel,type_id,selName){
        $("#"+selName+" option").each(function(){
            $(this).remove();
        });
        $("<option value=0>请选择</option>").appendTo($("#"+selName));
        if($("#"+sel).val()==0){
            return;
        }
        $.post(SCOPE.city_url,{pid:$("#"+sel).val(),type:type_id},
            function(data){
                if(data.code == 1001){
                    $.each(data.data,function(idx,item){
                        $("<option value="+item.id+">"+item.name+"</option>").appendTo($("#"+selName));
                    });
                }else{
                    $("<option value='0'>请选择</option>").appendTo($("#"+selName));
                }
            }
        );
    }

    $(function(){
        var commitStatus = false;
        /*表单验证*/
        $("#form-modal").validate({
            rules:{
                money:{
                    required:true,
                },
            },
            onkeyup:false,
            focusCleanup:true,
            success:"valid",
            submitHandler:function(form){
                var url = SCOPE.add_url;
                if(commitStatus==true){
                    return false;
                }
                var account_id = $("#account_id").val();
                if(account_id == 0){
                    layer.msg('请选择账户',{icon:2,time:2000});return false;
                }
                var pro_id = $("#pro_id").val();
                if(pro_id == 0){
                    layer.msg('请选择产品',{icon:2,time:2000});return false;
                }
                var province = $("#province").val();
                var city = $("#city").val();

                if(province != 0 && city == 0){
                    layer.msg('请选择城市',{icon:2,time:2000});return false;
                }
                /*表单提交*/
                $(form).ajaxSubmit({
                    type: 'post',
                    url: url,
                    success: function(data){
                        if(data.code == 1001){
                            commitStatus = true;
                            layer.msg(data.msg, {
                                icon: 1,
                                time: 2000
                            }, function(){
                                parent.location.reload();
                            });
                        }else{
                            layer.msg(data.msg,{icon:2,time:2000});
                        }
                    },
                    error: function(XmlHttpRequest, textStatus, errorThrown){
                        layer.msg(errorThrown,{icon:2,time:2000});
                    }
                });

            }
        });

        $("#form-modal2").validate({
            rules:{
                start_time:{
                    required:true
                },
                end_time:{
                    required:true
                }

            },
            onkeyup:false,
            focusCleanup:true,
            success:"valid",
            submitHandler:function(form){
                var url = SCOPE.add_timing_url;
                if(commitStatus==true){
                    return false;
                }
                /*表单提交*/
                $(form).ajaxSubmit({
                    type: 'post',
                    url: url,
                    success: function(data){
                        if(data.code == 1001){
                            commitStatus = true;
                            layer.msg(data.msg, {
                                icon: 1,
                                time: 2000
                            }, function(){
                                parent.location.reload();
                            });
                        }else{
                            layer.msg(data.msg,{icon:2,time:2000});
                        }
                    },
                    error: function(XmlHttpRequest, textStatus, errorThrown){
                        layer.msg(errorThrown,{icon:2,time:2000});
                    }
                });

            }
        });

        $("#form-modal3").validate({
            // rules:{
            //     api_name:{
            //         required:true
            //     },
            //     api_url:{
            //         required:true
            //     }
            //
            // },
            onkeyup:false,
            focusCleanup:true,
            success:"valid",
            submitHandler:function(form){
                var url = SCOPE.add_mapping_url;
                if(commitStatus==true){
                    return false;
                }
                /*表单提交*/
                $(form).ajaxSubmit({
                    type: 'post',
                    url: url,
                    success: function(data){
                        if(data.code == 1001){
                            commitStatus = true;
                            layer.msg(data.msg, {
                                icon: 1,
                                time: 2000
                            }, function(){
                                parent.location.reload();
                            });
                        }else{
                            layer.msg(data.msg,{icon:2,time:2000});
                        }
                    },
                    error: function(XmlHttpRequest, textStatus, errorThrown){
                        layer.msg(errorThrown,{icon:2,time:2000});
                    }
                });

            }
        });

        $("#form-modal4").validate({
            rules:{
                start_time2:{
                    required:true
                },
                end_time2:{
                    required:true
                }

            },
            onkeyup:false,
            focusCleanup:true,
            success:"valid",
            submitHandler:function(form){
                var url = SCOPE.add_timing_url2;
                if(commitStatus==true){
                    return false;
                }
                var chk_value =[];
                $('input[name="idcheckbox"]:checked').each(function(){ //遍历，将所有选中的值放到数组中
                    chk_value.push($(this).val());
                });
                /*表单提交*/
                $(form).ajaxSubmit({
                    type: 'post',
                    url: url,
                    data:{id:chk_value},
                    success: function(data){
                        if(data.code == 1001){
                            commitStatus = true;
                            layer.msg(data.msg, {
                                icon: 1,
                                time: 2000
                            }, function(){
                                parent.location.reload();
                            });
                        }else{
                            layer.msg(data.msg,{icon:2,time:2000});
                        }
                    },
                    error: function(XmlHttpRequest, textStatus, errorThrown){
                        layer.msg(errorThrown,{icon:2,time:2000});
                    }
                });

            }
        });

        $("#form-modal6").validate({
            rules:{

            },
            onkeyup:false,
            focusCleanup:true,
            success:"valid",
            submitHandler:function(form){
                var url = SCOPE.add_timeday2;
                if(commitStatus==true){
                    return false;
                }
                var chk_value =[];
                $('input[name="idcheckbox"]:checked').each(function(){ //遍历，将所有选中的值放到数组中
                    chk_value.push($(this).val());
                });
                /*表单提交*/
                $(form).ajaxSubmit({
                    type: 'post',
                    url: url,
                    data:{id:chk_value},
                    success: function(data){
                        if(data.code == 1001){
                            commitStatus = true;
                            layer.msg(data.msg, {
                                icon: 1,
                                time: 2000
                            }, function(){
                                parent.location.reload();
                            });
                        }else{
                            layer.msg(data.msg,{icon:2,time:2000});
                        }
                    },
                    error: function(XmlHttpRequest, textStatus, errorThrown){
                        layer.msg(errorThrown,{icon:2,time:2000});
                    }
                });

            }
        });

        $("#form-modal5").validate({
            rules:{
                // start_time2:{
                //     required:true
                // },
                // end_time2:{
                //     required:true
                // }

            },
            onkeyup:false,
            focusCleanup:true,
            success:"valid",
            submitHandler:function(form){
                var url = SCOPE.add_timeday;
                if(commitStatus==true){
                    return false;
                }

                /*表单提交*/
                $(form).ajaxSubmit({
                    type: 'post',
                    url: url,
                    success: function(data){
                        if(data.code == 1001){
                            commitStatus = true;
                            layer.msg(data.msg, {
                                icon: 1,
                                time: 2000
                            }, function(){
                                parent.location.reload();
                            });
                        }else{
                            layer.msg(data.msg,{icon:2,time:2000});
                        }
                    },
                    error: function(XmlHttpRequest, textStatus, errorThrown){
                        layer.msg(errorThrown,{icon:2,time:2000});
                    }
                });

            }
        });
    });

    function add_item(id) {
        $("input[name='id']").val(id);
        document.getElementById('form-modal').reset();
        $(".fw-mediumbold").text('添加');
    }

    function edit_item(id) {
        $.ajax({
            type: 'GET',
            url: SCOPE.item_url,
            dataType: 'json',
            data: {id:id},
            success: function(res){
                if(res.code == 1001){
                    $(".fw-mediumbold").text('编辑');
                    $("#id").val(res.data.id);
                    $("#account_id").val(res.data.account_id);
                    $("#pro_id").val(res.data.pro_id);
                    $("#money").val(res.data.money);
                    $("#province").val(res.data.prov);
                    if(res.data.city != ''){
                        $("#city").children().remove();
                        $("<option value='"+res.data.city+"'>"+res.data.city_name+"</option>").appendTo($("#city"));
                    }
                    $(":radio[name='status'][value='"+res.data.status+"']").prop("checked","checked");
                }else{
                    layer.msg(res.msg,{icon:2,time:2000});
                }

            },
            error:function(res) {
                layer.msg('网络错误,稍后再试...',{icon:2,time:2000});
            }
        });
    }

    function edit_mapping(id) {
        $("input[name='mer_id']").val(id);
        document.getElementById('form-modal3').reset();
        $.ajax({
            type: 'GET',
            url: SCOPE.item_mapping_url,
            dataType: 'json',
            data: {mer_id:id},
            success: function(res){
                if(res.code == 1001){
                    $("input[name='api_name']").val(res.data.api_name);
                    $("input[name='api_url']").val(res.data.api_url);
                    $("input[name='api_key']").val(res.data.api_key);
                    $.each(res.data.rules, function (index, value){
                        $("input[name='"+value.orig+"']").val(value.curr);
                        $("input[name='"+value.orig+"*mapping']").val(value.describe);
                    })
                }else if(res.code == 1000){

                }else{
                    layer.msg(res.msg,{icon:2,time:2000});
                }

            },
            error:function(res) {
                layer.msg('网络错误,稍后再试...',{icon:2,time:2000});
            }
        });
    }


    function edit_timing(id) {
        $.ajax({
            type: 'GET',
            url: SCOPE.item_url,
            dataType: 'json',
            data: {id:id},
            success: function(res){
                if(res.code == 1001){
                    $("#id2").val(res.data.id);
                    $("#start_time").val(res.data.start_time);
                    $("#end_time").val(res.data.end_time);
                }else{
                    layer.msg(res.msg,{icon:2,time:2000});
                }

            },
            error:function(res) {
                layer.msg('网络错误,稍后再试...',{icon:2,time:2000});
            }
        });
    }

    function edit_daytime(id) {
        $.ajax({
            type: 'GET',
            url: SCOPE.item_url,
            dataType: 'json',
            data: {id:id},
            success: function(res){
                if(res.code == 1001){
                    $("#id5").val(res.data.id);
                    $("#timeday").val(res.data.timeday);
                }else{
                    layer.msg(res.msg,{icon:2,time:2000});
                }

            },
            error:function(res) {
                layer.msg('网络错误,稍后再试...',{icon:2,time:2000});
            }
        });
    }



    /*删除*/
    function admin_del(obj,id){
        layer.confirm('确认要删除吗？',function(index){
            $.ajax({
                type: 'POST',
                url: SCOPE.delete_url,
                dataType: 'json',
                data: {id:id},
                success: function(data){
                    if(data.code == 1001){
                        $(obj).parents("tr").remove();
                        layer.msg('删除成功!',{icon:1,time:2000});
                    }else{
                        layer.msg(data.msg,{icon:2,time:2000});
                    }

                },
                error:function(data) {
                    layer.msg('网络错误,稍后再试...',{icon:2,time:2000});
                }
            });
        });
    }


    /*机构-停用*/
    function admin_stop(obj,id){
        layer.confirm('确认要停用吗？',function(index){
            //此处请求后台程序，下方是成功后的前台处理……
            var url = SCOPE.status_url;
            $.post(url,{status:0,id:id},function(data){
                if(data.code == 1001){
                    layer.msg('已停用!',{icon: 1,time:1000},function(){
                        window.location.reload();
                    });
                }else{
                    layer.msg(data.msg,{icon: 2,time:1000});
                }

            });


        });
    }

    /*机构-启用*/
    function admin_start(obj,id){
        layer.confirm('确认要启用吗？',function(index){
            //此处请求后台程序，下方是成功后的前台处理……

            var url = SCOPE.status_url;
            $.post(url,{status:1,id:id},function(data){
                if(data.code == 1001){

                    layer.msg('已启用!',{icon: 1,time:1000},function(){
                        window.location.reload();
                    });
                }else{
                    layer.msg(data.msg,{icon: 2,time:1000});
                }
            });
        });
    }

    /*短信重发*/
    function send_phone(obj,id){
        layer.confirm('确认要发送吗？',function(index){

            var loading = layer.load(0, {
                shade: true,
                time: 10*1000
            });

            //此处请求后台程序，下方是成功后的前台处理……
            var url = SCOPE.send_phone;
            $.post(url,{status:2,id:id},function(data){
                if(data.code == 1001){
                    layer.msg(data.msg,{icon: 1,time:1000},function(){
                        layer.close(loading);
                        window.location.reload();
                    });
                }else{
                    layer.close(loading);
                    layer.msg(data.msg,{icon: 2,time:2000});
                }

            });


        });
    }

    laydate.render({
        elem: '#start_time'
        ,type: 'datetime'
    });
    laydate.render({
        elem: '#end_time'
        ,type: 'datetime'
    });

    laydate.render({
        elem: '#start_time2'
        ,type: 'datetime'
    });
    laydate.render({
        elem: '#end_time2'
        ,type: 'datetime'
    });

    //时间范围
    laydate.render({
        elem: '#test9'
        ,type: 'time'
        ,range: true
    });

    laydate.render({
        elem: '#test10'
        ,type: 'time'
        ,range: true
    });

    function getCheckBoxVal(){ //jquery获取所有选中的复选框的值
        var chk_value =[];
        $('input[name="idcheckbox"]:checked').each(function(){ //遍历，将所有选中的值放到数组中
            chk_value.push($(this).val());
        });
    }

</script>


</html>




























